import {Component, Injector, Input, OnInit, Output} from '@angular/core';
import {NzModalService} from 'ng-zorro-antd/modal';
import {RosterUpdateComponent} from '../../../views/staffsComponents/roster/components/roster-update-modal/roster-update.component';
import {EmployeeServiceSvr} from '../../../services/employee.service';
import {CoreComponent} from 'pengesoft-ng-lib';

import {NzMessageService} from 'ng-zorro-antd/message';



@Component({
  selector: 'app-roster-table',
  templateUrl: './roster-table.component.html',
  styleUrls: ['./roster-table.component.less'],
})
export class RosterTableComponent extends  CoreComponent implements OnInit {

  @Input() checkState;
  @Input() sendRosterTableData;


  // tslint:disable-next-line:max-line-length
  listOfData: { skay: number; jobnumber: any; phone: any; sex: string; name: any; id: any; state: any; posting: any; department: any; age: any }[] = [];

  constructor(public modal: NzModalService,
              private injector: Injector,
              private employSvr: EmployeeServiceSvr,
              private message: NzMessageService) {
    super(injector);
  }

  ngOnInit(): void {
  }

  // 点了修改按钮
  edit(data): void {
    const editModal = this.modal.create({
      nzTitle: '修改',
      nzContent: RosterUpdateComponent,
      nzFooter: null,
      nzMask: false,
      nzComponentParams: {
        data,
      },
      nzWidth: '1000px'
    });
    // tslint:disable-next-line:no-shadowed-variable
    editModal.afterClose.subscribe((data) => {
      this.sendRosterTableData = [...data];
    });
  }

  // 点了离职按钮
  go(data): void {
    const editModal = this.modal.create({
      nzTitle: '添加离职',
      nzContent: `你即将吧${data.name}添加到待离职列表`,
      // nzFooter: null,
      nzMask: false,
      nzComponentParams: {
        data,
      },
      nzOnOk: () => {
        this.employSvr.update(data.id, '待离职').then(res => {
          if (res === 1) {

            this.message.success('操作成功！');
          }else {
            this.message.success('操作失败！');
          }
          // tslint:disable-next-line:no-shadowed-variable
          this.employSvr.myGetAllEmployDto().then(res => {
            this.sendRosterTableData = this.dealData(res);
            return this.sendRosterTableData;
          });
        });
      }
    });
  }

  // 调子组件传给父组件的值
  run2(msgData): void {
    this.sendRosterTableData = this.dealData(msgData);
  }

  // 返回数据处理
  dealData(msgData): any{
    return msgData.map((items, index) => {
      const sex = items.Employee.Sex === 0 ? '男' : '女';
      return {
        id: items.Employee.Id,
        skay: index + 1,
        posting: items.JobName,
        department: items.DeptName,
        jobnumber: items.Employee.Id,
        age: items.Employee.Age,
        sex,
        phone: items.Employee.Phone,
        state: items.Employee.StateId,
        name: items.Employee.Name,
        identifyCard: items.Employee.IDCard,
        identifyId: items.Employee.IDCard,
        school: items.Employee.GraduatedSchool,
        major: items.Employee.StudyMajor,
        degree: items.Employee.EducationLevel,
        political: items.Employee.Politic,
        native: items.Employee.NativePlace,
        emergencyPerPhone: items.Employee.EmergencyContact,
        emergencyPer: items.Employee.EmergencyPeople,
        employeeForm: items.Employee.EmployForm,
        cardNum: items.Employee.CardNum,
        IDPlace: items.Employee.IDPlace,
        NowPlace: items.Employee.NowPlace,
      };
    });
  }

}
